/**
 * 
 */
package org.swing.utility.common.window;

import java.lang.ref.WeakReference;

/**
 * @author lqnhu
 *
 */
public class UnwrappedWeakReference<T> extends WeakReference<T> {
	public UnwrappedWeakReference(T ref) {
		super(ref);
	}

	@Override
	public boolean equals(Object obj) {
		// unwrap both objects!
		T thisObject = this.get();
		// if the other object is also a WeakRefe
		if (obj instanceof WeakReference) {
			obj = ((WeakReference) obj).get();
		}
		// check if anything is null (was garbage collected)
		if (thisObject == null) {
			if (obj == null) {
				return true;
			} else {
				return false;
			}
		}
		return thisObject.equals(obj);
	}

	@Override
	public int hashCode() {
		// unwrap me!
		T thisObject = this.get();
		// check if anything is null (was garbage collected)
		if (thisObject == null) {
			return 0;
		} else {
			return thisObject.hashCode();
		}
	}
}
